package com.tcoiss.datafactory.controller;

import com.tcoiss.common.core.utils.SecurityUtils;
import com.tcoiss.common.core.utils.poi.ExcelUtil;
import com.tcoiss.common.core.web.controller.BaseController;
import com.tcoiss.common.core.web.domain.AjaxResult;
import com.tcoiss.common.core.web.page.TableDataInfo;
import com.tcoiss.common.log.annotation.Log;
import com.tcoiss.common.log.enums.BusinessType;
import com.tcoiss.common.security.annotation.PreAuthorize;
import com.tcoiss.datafactory.domain.ExecuteWork;
import com.tcoiss.datafactory.service.IExecuteWorkService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * 作业Controller
 * 
 * @author zw
 * @date 2021-04-26
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/work" )
public class ExecuteWorkController extends BaseController {

    private final IExecuteWorkService iExecuteWorkService;

    /**
     * 查询作业列表
     */
    @PreAuthorize(hasPermi = "app:work:list")
    @GetMapping("/list")
    public TableDataInfo list(ExecuteWork executeWork) {
        startPage();
        List<ExecuteWork> list = iExecuteWorkService.queryList(executeWork);
        return getDataTable(list);
    }

    /**
     * 导出作业列表
     */
    @PreAuthorize(hasPermi = "app:work:export" )
    @Log(title = "作业" , businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response,ExecuteWork executeWork) throws IOException
    {
        List<ExecuteWork> list = iExecuteWorkService.queryList(executeWork);
        ExcelUtil<ExecuteWork> util = new ExcelUtil<ExecuteWork>(ExecuteWork.class);
        util.exportExcel(response,list, "work" );
    }

    /**
     * 获取作业详细信息
     */
    @PreAuthorize(hasPermi = "app:work:query" )
    @GetMapping(value = "{workId}" )
    public AjaxResult getInfo(@PathVariable("workId" ) Long workId) {
        ExecuteWork executeWork = iExecuteWorkService.getByWorkId(workId);
        return AjaxResult.success(executeWork);
    }
    /**
     * 获取当前方案的作业优先级最大值
     */
    @PreAuthorize(hasPermi = "app:work:query" )
    @GetMapping(value = "/getWorkOderMax" )
    public AjaxResult getWorkOderMax(Long schemeId) {
        Integer max = iExecuteWorkService.getWorkOderMax(schemeId);
        return AjaxResult.success(max);
    }

    /**
     * 获取当前方案的作业优先级列表
     */
    @PreAuthorize(hasPermi = "app:work:query")
    @GetMapping(value = "/getPriority")
    public AjaxResult getPriority(Long schemeId){
        Integer[] priorityArray = iExecuteWorkService.getPriorityById(schemeId);
        return AjaxResult.success(priorityArray);
    }

    /**
     *  更新使用状态
     */
    @PreAuthorize(hasPermi = "app:work:edit")
    @GetMapping(value = "/updateStatus")
    public AjaxResult updateStatus(Long[] workIds, Integer status){
        return AjaxResult.success(iExecuteWorkService.updateStatus(Arrays.asList(workIds),status) ? 1 : 0);
    }


    /**
     * 新增作业
     */
    @PreAuthorize(hasPermi = "app:work:add" )
    @Log(title = "作业" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ExecuteWork executeWork) {
        executeWork.setCreatorName(SecurityUtils.getUsername());
        return toAjax(iExecuteWorkService.save(executeWork) ? 1 : 0);
    }

    /**
     * 修改作业
     */
    @PreAuthorize(hasPermi = "app:work:edit" )
    @Log(title = "作业" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ExecuteWork executeWork) {
        return toAjax(iExecuteWorkService.updateWorkById(executeWork) ? 1 : 0);
    }

    /**
     * 修改优先级
     */
    @PreAuthorize(hasPermi = "app:work:edit" )
    @Log(title = "作业优先级修改" , businessType = BusinessType.UPDATE)
    @PutMapping(value = "/setWorkOrder")
    public AjaxResult setWorkOrder(@RequestBody ExecuteWork[] executeWork) {
        return toAjax(iExecuteWorkService.saveOrUpdateBatch(Arrays.asList(executeWork)) ? 1 : 0);
    }

    /**
     * 删除作业
     */
    @PreAuthorize(hasPermi = "app:work:remove" )
    @Log(title = "作业" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{workIds}" )
    public AjaxResult remove(@PathVariable Long[] workIds) {
        return toAjax(iExecuteWorkService.removeByIds(Arrays.asList(workIds)) ? 1 : 0);
    }
}
